<?php

use Biz\model\store\StoreBasic as mStoreBasic;
use Biz\service\BrandPreFecture;
use Biz\service\Store\StoreBasicService;
use Biz\tools\Location;
use Doraemon\cache\UserCounter;
use Doraemon\service\statSign\Basic as statSign;
use Doraemon\tools\BlockType;
use Doraemon\tools\HhzClient;
use Doraemon\tools\Hhzurl;
use Biz\cache\brand\BrandBeLikedache;

/**
 * 好物频道头部信息
 * Class ChannelHeadAction
 */
class BrandStoreListAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    private $data = [];

    public function main()
    {
        $this->getBanner();
        $this->getBrandList();
        //$this->getStoreList(); //门店业务下线
        $this->data['more_brand_link'] = 'niceliving://webview/fullscreen?protego=' . urlencode(base64_encode(json_encode(['url' => Hhzurl::getDomain() . '/brand/brand-zone'])));
        //$this->data['more_store_link'] = 'niceliving://webview/normalscreen?protego='.urlencode(base64_encode(json_encode(['url' => Hhzurl::getDomain().'/store/pick-store-list'])));//门店业务下线
        $this->data = $this->data ? $this->data : (object)[];
        $this->setSuccess($this->data);
    }


    private function getBrandList()
    {
        $brandList = BrandPreFecture::recommendBrandsForChannelHead('好物主频道精选品牌', $this->uid);
        if (!empty($brandList)) {
            if (version_compare(HhzClient::getClientVersion(), '6.20.0', '>=')) {
                $brandIds = [];
                foreach ($brandList as $row) {
                    $brandIds[] = $row['brand']['user_info']['brand']['brand_id'];
                }
                $userLikesArr = $brandIds ? BrandBeLikedache::muilteGetUserLike($brandIds) : [];
                $i = 1;
                foreach ($brandList as &$row) {
                    $brandId = $row['brand']['user_info']['brand']['brand_id'];
                    $userLike = $userLikesArr[$brandId] ?? 0;
                    $userLike = $userLike + (isset($row['brand']['counter']['beliked']) ? $row['brand']['counter']['beliked'] : 0) + (isset($row['brand']['counter']['befavorited']) ? $row['brand']['counter']['befavorited'] : 0);
                    $row['brand']['counter']['user_like'] = $userLike;
                    $row['statSign'] = statSign::genStatSign(statSign::MODULE_FINE_GOODS_BRAND_CARD, [
                        statSign::EXTEND_KEY_OBJ_ID => $row['brand']['user_info']['brand']['brand_id'],
                        statSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_BRAND,
                        statSign::EXTEND_KEY_INDEX => $i
                    ]);
                    $i++;
                }
            }
            $this->data['brand_list'] = $brandList;
        }
    }

    private function getStoreList()
    {
        $list = [];
        $location = new Location($this->uid);
        if ($location->isShowStore()) {
            $areaCode = $location->getAreaCode();
            $where = ['area_code' => $areaCode, 'is_test' => 0];
            $storeBasicInfos = mStoreBasic::getStoreByFilter($where);
            $uids = array_column($storeBasicInfos, 'uid');
            $userCounters = UserCounter::getMultiByUids($uids);
            $storeList = StoreBasicService::formateBasicList($storeBasicInfos);
            $sortArr = [];
            foreach ($storeList as $key => $row) {
                $said = isset($userCounters[$row['uid']]) ? ($userCounters[$row['uid']]['liver_said'] + $userCounters[$row['uid']]['designer_said']) : 0;
                $storeList[$key]['said'] = $said;
                $sortArr[$key] = $said;
            }
            $sortArr ? array_multisort($sortArr, SORT_DESC, $storeList) : [];
            foreach ($storeList as $row) {
                unset($row['said']);
                $row["statSign"] = statSign::genStatSign(statSign::MODULE_FINE_GOODS_MAIN_RECOMM_STORES, [
                    statSign::EXTEND_KEY_OBJ_ID => $row['id'],
                    statSign::EXTEND_KEY_OBJ_TYPE => BlockType::TYPE_STORE,
                ]);
                $list[] = $row;
            }
        }
        if (!empty($list)) {
            $store_list = $list ? array_slice($list, 0, 8) : [];
            foreach ($store_list as $val) {
                $statSign = $val['statSign'];
                unset($val['statSign']);
                $store_info = $val;
                $this->data['store_list'][] = [
                    'type' => BlockType::TYPE_STORE,
                    'store' => [
                        'store_info' => $store_info,
                    ],
                    'statSign' => $statSign
                ];
            }

        }
    }

    /**
     * 品牌情报局
     * @return void
     */
    private function getBanner()
    {
        $index = rand(1, 3);
        $url = Hhzurl::getDomain() . '/brand/brand-news';
        if (version_compare(HhzClient::getClientVersion(), '6.20.0', '>=')) {
            $this->data['banner'] = [
                'banner_url' => 'https://img.haohaozhu.cn/Op-imageShow/8d06630ku0ag0O100rx7mgy?iv=1',
                'link' => 'niceliving://webview/fullscreen?protego=' . urlencode(base64_encode(json_encode(['url' => $url]))),
                'statSign' => statSign::genStatSign(statSign::MODULE_FINE_GOODS_BRAND_IP_CARD)
            ];
        }
    }
}